<template>
  <div class="login">
      <h3>账号密码登录</h3>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="账号" prop="username">
    <el-input v-model.number="ruleForm.username"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>

  
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
    <p><button @click="goregister">立即注册</button></p>
    
    
  </el-form-item>
</el-form>
  </div>
</template>
<script>
  export default {
    data() {
      var checkUser = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入用户名'));
        }else{
          callback();
        }
          

      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      
      return {
        ruleForm: {
          pass: '',
          username: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          username: [
            { validator: checkUser, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      goregister:function(){
        this.$router.push({
          name:'Reigist'
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch("login",{username:this.ruleForm.username,password:this.ruleForm.pass}).then(res=>{
              if(res.code==200){
                this.$router.push("/")
              }else{
                alert(res.msg)
              }
            })
            // login({username:this.ruleForm.username,password:this.ruleForm.pass}).then(res=>{
            //   if(res.code==200){
            //     this.$router.push("/")
            //   }else{
            //     alert(res.msg)
            //   }
            // })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
     
    }
  }
</script>
<style lang="scss">
//   

    .login{
        margin-top: 20vh;
        .el-form-item{
            margin-right: 20px;
        }
        .el-form-item__label{
            padding-right: 30px;
        }
        h3{
            text-align: center;
            line-height: 150px;
            font-weight: normal;
            font-size: 30px;
        }
        .el-button{
            width: 90%;
            height: 40px;
            background-color: orangered;
            border-radius:30px;
          
        }
        p{
          display: block;
          width: 100px;
          line-height: 30px;
        }
     

    }
</style>